$(function() {
    echart1() //设计师详情分析
    echart2() //订单分析中心
    kanban() //数据中心
    echart3() //设计颜色
    echart4() //七月订单走势图
    echart5() //订单季组占比图
    echart6() //订单、生产、裁剪、出货走势图

    function echart1() {
        AJAX("http://139.9.245.84:8300/rfCRUD/BiSQLSeleData", {
                sql: "select mpsid,SUM(allqty) allqty FROM er_mps_qty WHERE mpsid in(select mpsid from er_mps WHERE is_order='1') GROUP BY mpsid",
                T: 1
            }, 'GET', null, null,
            (res) => {
                console.log(res)
                var data = res.data;
                var data2 = []; // 款号
                var data3 = []; // 柱形图数据
                var a = 5; //计算器
                for (var i = 0; i < 5; i++) {
                    data2.push(data[i].mpsid)
                    data3.push(data[i].allqty)
                }
                console.log(data2)
                var app = {};
                var chartDom = document.getElementById('echart1');
                var myChart = echarts.init(chartDom);
                var option;

                option = {
                    title: {
                        text: '动态数据',
                        subtext: '',
                        textStyle: {
                            color: 'rgba(255,255,255,0.8)'
                        }
                    },
                    tooltip: {
                        trigger: 'axis',
                        axisPointer: {
                            type: 'cross',
                            label: {
                                backgroundColor: '#283b56'
                            }
                        }
                    },
                    legend: {
                        data: '',
                        textStyle: {
                            color: 'rgba(255,255,255,0.8)'
                        }
                    },
                    toolbox: {
                        show: true,
                        feature: {
                            dataView: { readOnly: false },
                            restore: {},
                            saveAsImage: {}
                        }
                    },
                    dataZoom: {
                        show: true,
                        start: 0,
                        end: 100
                    },
                    xAxis: [{
                            type: 'category',
                            boundaryGap: true,
                            data: data2
                        },
                        {
                            type: 'category',
                            boundaryGap: true,
                            data: (function() {
                                var res = [];
                                var len = 5;
                                while (len--) {
                                    res.push(10 - len - 1);
                                }
                                return res;
                            })()
                        }
                    ],
                    yAxis: [{
                            type: 'value',
                            scale: true,
                            name: '',
                            max: 100,
                            min: 0,
                            boundaryGap: [0.2, 0.2]
                        },
                        {
                            type: 'value',
                            scale: false,
                            name: '总量',
                            max: 100,
                            min: 0,
                            boundaryGap: [0.2, 0.2]
                        }
                    ],
                    series: [{
                        name: '版单',
                        type: 'bar',
                        xAxisIndex: 1,
                        yAxisIndex: 1,
                        itemStyle: {
                            color: '#20B2AA'
                        },
                        label: {
                            show: true,
                            position: 'top',
                            valueAnimation: true
                        },
                        data: data3
                    }]
                };

                app.count = 11;
                setInterval(function() {
                    // var axisData = (new Date()).toLocaleTimeString().replace(/^\D*/, '');


                    // var data1 = option.series[1].data;
                    data3.shift();
                    data3.push(data[a].allqty)
                        // data1.shift();
                        // data1.push((Math.random() * 10 + 5).toFixed(1) - 0);
                    option.xAxis[0].data.shift();
                    option.xAxis[0].data.push(data[a].mpsid)
                    option.xAxis[1].data.shift();
                    option.xAxis[1].data.push(app.count++);
                    a = a + 1;
                    if (a >= data.length) {
                        a = 0
                    }

                    myChart.setOption(option);
                }, 2100);

                myChart.setOption(option);
            }, (e) => {})
    }

    function echart2() {
        AJAX("http://139.9.245.84:8300/rfCRUD/BiSQLSeleData", {
                sql: "select mpsid,SUM(allqty) allqty FROM er_mps_qty WHERE mpsid in(select offerid from er_mps WHERE is_order='2') GROUP BY mpsid",
                T: 1
            }, 'GET', null, null,
            (res) => {
                console.log(res)
                var data = res.data;
                var data2 = []; // 款号
                var data3 = []; // 柱形图数据
                var a = 4; //计算器
                var len = data.length;
                for (var i = 0; i < 5; i++) {
                    data2.push(data[i].mpsid)
                    data3.push(data[i].allqty)
                }
                console.log(data2)
                var app = {};
                var chartDom = document.getElementById('echart2');
                var myChart = echarts.init(chartDom);
                var option;

                option = {

                    tooltip: {
                        trigger: 'axis',
                        axisPointer: {
                            type: 'cross',
                            label: {
                                backgroundColor: '#283b56'
                            }
                        }
                    },
                    legend: {
                        data: '',
                        textStyle: {
                            color: 'rgba(255,255,255,0.8)'
                        }
                    },
                    toolbox: {
                        show: true,
                        feature: {
                            dataView: { readOnly: false },
                            restore: {},
                            saveAsImage: {}
                        }
                    },
                    dataZoom: {
                        show: false,
                        start: 0,
                        end: 100
                    },
                    xAxis: [{
                            type: 'category',
                            boundaryGap: true,
                            data: data2
                        },
                        {
                            type: 'category',
                            boundaryGap: true,
                            data: (function() {
                                var res = [];
                                var len = 5;
                                while (len--) {
                                    res.push(10 - len - 1);
                                }
                                return res;
                            })()
                        }
                    ],
                    yAxis: [{
                            type: 'value',
                            scale: true,
                            name: '',
                            max: 5,
                            min: 0,
                            boundaryGap: [0.2, 0.2]
                        },
                        {
                            type: 'value',
                            scale: true,
                            name: '总量',
                            max: 1000000,
                            min: 0,
                            boundaryGap: [0.2, 0.2]
                        }
                    ],
                    series: [{
                        name: '订单',
                        type: 'bar',
                        xAxisIndex: 1,
                        yAxisIndex: 1,
                        label: {
                            show: true,
                            position: 'top',
                            valueAnimation: true
                        },
                        data: data3
                    }]
                };

                app.count = 11;
                setInterval(function() {
                    // var axisData = (new Date()).toLocaleTimeString().replace(/^\D*/, '');
                    a = a + 1;
                    if (a >= data.length) {
                        a = 0
                    }

                    // var data1 = option.series[1].data;
                    data3.shift();
                    data3.push(data[a].allqty)
                        // data1.shift();
                        // data1.push((Math.random() * 10 + 5).toFixed(1) - 0);
                    option.xAxis[0].data.shift();
                    option.xAxis[0].data.push(data[a].mpsid)
                    option.xAxis[1].data.shift();
                    option.xAxis[1].data.push(app.count++);

                    myChart.setOption(option);
                }, 2100);

                myChart.setOption(option);
            }, (e) => {})
    }

    function kanban() {
        AJAX("http://139.9.245.84:8300/rfCRUD/BiSQLSeleData", {
                sql: "select SUM(allqty) allqty FROM er_mps_qty WHERE mpsid in(select offerid from er_mps WHERE is_order='2' AND creation_date BETWEEN '2020-01-01' AND '2022-01-01') UNION ALL select SUM(allqty) allqty FROM er_mps_qty WHERE mpsid in(select mpsid from er_mps WHERE is_order='1' AND creation_date BETWEEN '2020-01-01' AND '2022-01-01')",
                T: 1
            }, 'GET', null, null,
            (res1) => {
                var data1 = res1.data;
                AJAX("http://139.9.245.84:8300/rfCRUD/BiSQLSeleData", {
                        sql: "select qty from er_cut_tab WHERE cutid IN(select cutid from er_cut WHERE creation_date BETWEEN '2020-01-01' AND '2022-01-01')",
                        T: 1
                    }, 'GET', null, null,
                    (res2) => {
                        console.log("data2", res2.data)
                        var data2 = res2.data;
                        var data4 = 0;
                        var a = new Mrchao() //实例化对象
                        var data3 = a.arrayadd(data2, "qty")
                        for (var i = 0; i < data3.length; i++) {
                            data4 = data4 + data3[i]
                        }
                        console.log("data4", data4)
                        $("#allqty1").text(data1[0].allqty);
                        $("#allqty2").text(data1[1].allqty);
                        $("#allqty3").text(data4);

                    }, (e) => {})
            }, (e) => {})
    }

    function echart3() {
        AJAX("http://139.9.245.84:8300/rfCRUD/BiSQLSeleData", {
                sql: "select color_name name,count(id) value from er_mps_design  GROUP BY color_name",
                T: 1
            }, 'GET', null, null,
            (res1) => {
                var data = res1.data;
                var chartDom = document.getElementById('echart3');
                var myChart = echarts.init(chartDom);
                var option;

                option = {

                    toolbox: {
                        show: true,
                        feature: {
                            mark: { show: true },
                            dataView: { show: true, readOnly: false },
                            restore: { show: true },
                            saveAsImage: { show: true }
                        }
                    },
                    series: [{
                        name: '面积模式',
                        type: 'pie',
                        radius: [20, 90],
                        center: ['50%', '50%'],
                        roseType: 'area',
                        label: {
                            textStyle: {
                                color: 'rgba(255,255,255,1)'
                            }
                        },
                        itemStyle: {
                            borderRadius: 8
                        },
                        data: data
                    }]
                };

                option && myChart.setOption(option);
            }, (e) => {})

    }

    function echart4() {
        var a = new Mrchao();
        var sqlstr = a.sql();
        var time = a.arrMonth()
        console.log("sqlstr", sqlstr)
        AJAX("http://139.9.245.84:8300/rfCRUD/BiSQLSeleData", {
                sql: sqlstr,
                T: 1
            }, 'GET', null, null,
            (res1) => {
                var data = res1.data;
                var data1 = [];
                for (var i = 0; i < data.length; i++) {
                    if (data[i].allqty !== undefined) {
                        data1.push(data[i].allqty)
                    } else {
                        data1.push(0)
                    }
                }
                console.log(data1)

                var chartDom = document.getElementById('echart4');
                var myChart = echarts.init(chartDom);
                var option;

                option = {
                    xAxis: {
                        type: 'category',
                        boundaryGap: false,
                        data: time[2] // 日期数据
                    },
                    yAxis: {
                        type: 'value'
                    },
                    series: [{
                        data: data1, // 订单数据
                        type: 'line',
                        label: {
                            show: true,
                            position: 'top',
                            valueAnimation: true
                        },
                        itemStyle: {
                            color: '#48D1CC'
                        },
                        areaStyle: {}
                    }]
                };

                option && myChart.setOption(option);
            }, (e) => {})
    }

    function echart5() {
        AJAX("http://139.9.245.84:8300/rfCRUD/BiSQLSeleData", {
                sql: "select er_mps.season name,SUM(er_mps_qty.allqty) value FROM er_mps LEFT JOIN er_mps_qty ON er_mps.offerid=er_mps_qty.mpsid where er_mps.is_order='2'  GROUP BY er_mps.season",
                T: 1
            }, 'GET', null, null,
            (res) => {
                var data = res.data;
                var colorarr = [{
                        itemStyle: { color: '#00B2EE' }
                    },
                    {
                        itemStyle: { color: '#00C5CD' }
                    },
                    {
                        itemStyle: { color: '#1C86EE' }
                    },
                    {
                        itemStyle: { color: '#1C86EE' }
                    },
                    {
                        itemStyle: { color: '#00BFFF' }
                    },

                    {
                        itemStyle: { color: '#00E5EE' }
                    }
                ]
                for (var i = 0; i < data.length; i++) {
                    data[i].itemStyle = colorarr[i].itemStyle
                }
                console.log("++++++++++++++++++++", data)
                var chartDom = document.getElementById('echart5');
                var myChart = echarts.init(chartDom);
                var option;
                option = {
                    backgroundColor: 'rgba(28,134,238,0.2)',
                    title: {
                        left: 'center'
                    },
                    tooltip: {
                        trigger: 'item'
                    },
                    legend: {
                        orient: 'horizontal',
                        top: 'top',
                        textStyle: {
                            color: 'rgba(250,250,250,0.5)'
                        }
                    },
                    series: [{
                        name: '访问来源',
                        type: 'pie',
                        radius: '50%',
                        data: data,
                        emphasis: {
                            itemStyle: {
                                shadowBlur: 10,
                                shadowOffsetX: 0,
                                shadowColor: 'rgba(0, 0, 0, 0.5)'
                            }
                        }
                    }]
                };

                option && myChart.setOption(option);
            }, (e) => {})

    }

    function echart6() {
        var date = new Date()
        var year = date.getFullYear() //获取今年的年
        var month = date.getMonth() //获取今年的月
        var yearmonth = date.getFullYear() + '-' + date.getMonth() //获取今年的年-月
        var mondata1 = [], //订单数据
            mondata2 = [], //生产数据
            mondata3 = []; //日期数据
        //select allqty,creation_date,is_order FROM er_mps_qty WHERE mpsid in(select offerid from er_mps WHERE is_order='0' OR is_order='2'  AND creation_date like '2021%')
        AJAX("http://139.9.245.84:8300/rfCRUD/BiSQLSeleData", {
                sql: "select allqty,creation_date,is_order FROM er_mps_qty WHERE mpsid in(select offerid from er_mps WHERE is_order='0' OR is_order='2'  AND creation_date like '" + year + "%')",
                T: 1
            }, 'GET', null, null,
            (res1) => {
                var data = res1.data;
                for (var i = 0; i <= month; i++) {
                    mondata1[i] = 0
                    mondata2[i] = 0
                    var month1 = month + 1 - i
                    if (month1 < 10) {
                        month1 = '0' + month1
                    }
                    mondata3.unshift((year + '-' + month1))
                    console.log((year + '-' + month1))
                    var patt1 = new RegExp((year + '-' + month1))
                    for (var j = 0; j < data.length; j++) {
                        if (patt1.test(data[j].creation_date) && data[j].is_order == "2") {
                            mondata1[i] = mondata1[i] + data[j].allqty
                        } else if (patt1.test(data[j].creation_date) && data[j].is_order == "0") {
                            mondata2[i] = mondata2[i] + data[j].allqty
                        }
                    }
                }
                console.log(mondata1)
                console.log(mondata2)
                console.log("================", data)
                var chartDom = document.getElementById('echart6');
                var myChart = echarts.init(chartDom);
                var option;

                option = {
                    backgroundColor: 'rgba(28,134,238,0.2)',
                    title: {
                        text: ''
                    },
                    tooltip: {
                        trigger: 'axis'
                    },
                    legend: {
                        data: ['订单', '生产'],
                        textStyle: {
                            color: 'rgba(255,255,255,0.5)'
                        }
                    },
                    grid: {
                        left: '3%',
                        right: '4%',
                        bottom: '3%',
                        containLabel: true
                    },
                    toolbox: {
                        feature: {
                            saveAsImage: {}
                        }
                    },
                    xAxis: {
                        type: 'category',
                        boundaryGap: false,
                        data: mondata3
                    },
                    yAxis: {
                        type: 'value'
                    },
                    series: [{
                            name: '订单',
                            type: 'line',
                            data: mondata1, //订单数据
                            label: {
                                show: true,
                                position: 'top',
                                valueAnimation: true
                            }
                        },
                        {
                            name: '生产',
                            type: 'line',
                            data: mondata2, //生产数据
                            label: {
                                show: true,
                                position: 'top',
                                valueAnimation: true
                            }
                        }
                    ]
                };

                option && myChart.setOption(option);

            }, () => {})
    }
})